<div class="limited" style="width: 1400px;"><div id="form"></div></div>
<script>

var inputs=[
	{"type":"accordion","name":"blocks",mode:"blocks","label":"",pages:[
			{id:"main-block",name:"{{!Main information}}"},
			{id:"columns-block",name:"{{!Database fields}}"},
			{id:"columns-l10n-block",name:"{{!Database L10N fields}}"},
			{id:"columns-extra-block",name:"{{!Extra fields}}"},
			{id:"adminpanel-block",name:"{{!Admin-panel}}"},
			{id:"list-sql-block",name:"{{!Prebuilt SQL queries}}"}
		]
	},
	{type:"label",name:"name",label:"{{!Model name}}",width:500,value:{{JSON:model.name}},xpath:".main-block"},
	{type:"label",name:"table",label:"{{!Table name}}",width:500,value:{{JSON:model.table}},xpath:".main-block"},
	{type:"label",name:"folder",label:"{{!Folder name}}",width:500,value:{{JSON:model.folder}},xpath:".main-block"},
	{{foreach l in model.patches.lists}}
	{type:"model_listinfo",name:"model_listinfo_{{l.name}}",value:{{JSON:l}},xpath:".list-sql-block"},
	{{/foreach}}
	{type:"checkbox2",name:"adminpanel_enabled",label:"{{!Admin panel}}",value:{{JSON:model.adminpanel.enabled}},xpath:".adminpanel-block",showwidgets:["adminpanel_columns","adminpanel_role","adminpanel_section","adminpanel_label","adminpanel_cols_through","adminpanel_listname","adminpanel_edithelperfunc","adminpanel_savehelperfunc"]},
	{type:"textedit",name:"adminpanel_role",label:"{{!Access role}}",width:500,value:{{JSON:model.adminpanel.role}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_section",label:"{{!Navigation section}}",width:500,value:{{JSON:model.adminpanel.section}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_label",label:"{{!Navigation label}}",width:500,value:{{JSON:model.adminpanel.label}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_cols_through",label:"{{!Go-through-columns}}",width:500,value:{{JSON:model.adminpanel.cols_through}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_listname",label:"{{!SQL list name}}",width:500,value:{{JSON:model.adminpanel.listname}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_listhelperfunc",label:"{{!List helper function}}",width:500,value:{{JSON:model.adminpanel.listhelperfunc}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_edithelperfunc",label:"{{!Edit helper function}}",width:500,value:{{JSON:model.adminpanel.edithelperfunc}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_savehelperfunc",label:"{{!Save helper function}}",width:500,value:{{JSON:model.adminpanel.savehelperfunc}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_prehelperfunc",label:"{{!Pre helper function}}",width:500,value:{{JSON:model.adminpanel.prehelperfunc}},xpath:".adminpanel-block"},
	{type:"textedit",name:"adminpanel_impexphelperfunc",label:"{{!Import/export helper function}}",width:500,value:{{JSON:model.adminpanel.impexphelperfunc}},xpath:".adminpanel-block"},
	{
		type:"grid",title:"{{!Column settings}}",name:"adminpanel_columns",mode:"form-edit-full",maxheight:0,
		rowClasses: [{
			name: "column",
			columns: [
				{name:"name",label:"Column name",width:150,readOnly:1},
				{name:"show_list",label:"Show in list",width:100,type:"checkbox"},
				{name:"show_edit",label:"Show in edit",width:100,type:"checkbox"},
				{name:"show_create",label:"Show in create",width:100,type:"checkbox"},
				{name:"width_list",label:"in-list width",width:100}
			]
		}],
		rows:[],
		xpath:".adminpanel-block"
	}
	
];
var newid=1;
var frm=$("#form").ajajform({
	header: "{{!Tune model}}",
	submit: {
		url: '{{action}}',
		value: "{{!Save}}",
		ajaj: true,
		params: {"mode":"tune_save","id":"{{model.name}}","ajaj":1},
	},
	footerButtons: [
		{value:"{{!Add SQL list}}", click: function() {frm.ajajform("insertWidget",null,null,{type:"model_listinfo",name:"model_listinfo_"+(newid++),value:{},xpath:".list-sql-block"})}},
		{value:"{{!Add extra field}}", click: function() {extra_fields_grid.ajajform_grid("insertRow",null,{rowClass: "field",cells:["","",0]});}},
		{value:"{{!Delete extra fields}}", click: function() {grid.ajajform_grid("deleteSelectedRownums");}}
	]
});
for (var i=0;i<inputs.length;i++) {
	frm.ajajform("insertWidget",inputs[i].tabname,null,inputs[i]);
}

var extra_fields_grid=frm.ajajform("insertWidget",null,null,{
		type:"grid",title:"",name:"extra_fields",mode:"form-edit-full",maxheight:0,rowSelection: true,xpath:".columns-extra-block",
		rowClasses: [{
			name: "field",
			columns: [
				{name:"name",label:"Name",width:250},
				{name:"datatype",label:"Datatype",width:250},
				{name:"L10N",label:"L10N",width:100,type:"checkbox"}
			]
		}],
		post: "json",
		rows: []
	});
var extra_fields={{JSON:modelcopy.extra_fields}};
for (var i=0;i<extra_fields.length;i++) {
	var pl=extra_fields[i];
	extra_fields_grid.ajajform_grid("insertRow",null,{rowClass: "field",cells:[pl.name,pl.datatype,pl.l10n],id:pl.name});
}
function grid_columns_init(l10n,arr)
{
	var grid=frm.ajajform("insertWidget",null,null,{
		type: "grid",
		rowSelection: false,
		title: "",
		post: "json",
		name: "grid_columns"+(l10n?"_l10n":""),
		mode: "form-edit-full",
		maxheight:0,
		rowClasses: [{
			name:"column",
				columns: [
				{name: "name", label: "Column name", width: 150,readOnly:1},
				{name: "datatype", label: "Datatype", width: 100, readOnly:1},
				{name: "not_null", label: "Not null", width: 100, type:"checkbox", readOnly:1},
				{name: "table", label: "FK table", width: 150, readOnly:1},
				{name: "changed_datatype", label: "Change datatype", width: 150}
			]
		}],
		rows:[],
		xpath: ".columns"+(l10n?"-l10n":"")+"-block"
	});
	for (var i=0;i<arr.length;i++) {
		var pl=arr[i];
		grid.ajajform_grid("insertRow",null,{rowClass: "column",cells:[pl._orig_name,pl._orig_datatype,pl._orig_not_null,pl._orig_table,pl._changed_datatype],id:pl.name});
	}
	return grid;
}
var grid1=grid_columns_init(0,{{JSON:modelcopy.columns}});
var grid2=grid_columns_init(1,{{JSON:modelcopy.columns_l10n}});



</script>

